package com.zoke.daycarpool.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.umeng.fb.FeedbackAgent;
import com.umeng.fb.model.Conversation;
import com.umeng.fb.model.DevReply;
import com.umeng.fb.model.Reply;
import com.zoke.daycarpool.R;
import com.zoke.daycarpool.adapter.FeeBackAdapter;
import com.zoke.daycarpool.base.BaseUi;
import com.zoke.daycarpool.domain.FeedBackBeen;
import com.zoke.support.base.BaseApp;
import com.zoke.support.db.JackDb;
import com.zoke.support.utils.CommonUtil;
import com.zoke.support.utils.LogUtil;
import com.zoke.support.view.XListView;
import com.zoke.support.view.ZokeToast;

/**
 * 用戶反饋 -- 意见反馈
 * 
 * @author jack
 * 
 */
public class FeedBack extends BaseUi implements Conversation.SyncListener {
	private FeedbackAgent mFeedAgent;
	private Conversation mConversion;
	private XListView mReplyListView;
	private EditText mPublicFeedBack;

	private final List<FeedBackBeen> mList = new ArrayList<FeedBackBeen>();

	private FeeBackAdapter mAdapter;
	private JackDb db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback);
		// 获取反馈代理
		mFeedAgent = new FeedbackAgent(this);
		mFeedAgent.sync();
		mConversion = mFeedAgent.getDefaultConversation();
		mTitleManager.setTitle("天天拼车客服").setLeftIcon(R.drawable.title_back);
		mEnableSwipeRight = true;
		mPublicFeedBack = (EditText) findViewById(R.id.edit_feedback);
		mReplyListView = (XListView) findViewById(R.id.xListView_feedback);
		mReplyListView.setPullLoadEnable(false);
		mReplyListView.setPullRefreshEnable(false);
		// 初始化适配器的数据源
		mAdapter = new FeeBackAdapter(FeedBack.this, mList);
		// 为反馈列表 设置适配器
		mReplyListView.setAdapter(mAdapter);
		FeedBackBeen feed = new FeedBackBeen();
		feed.setType(0);
		feed.setContent("我是小天，您有什么问题？");
		feed.setTime(new Date().getTime());
		mList.add(feed);
		// 调用友盟的获取数据
		mConversion.sync(this);
		BaseApp app = (BaseApp) this.getApplicationContext();
		db = app.createDb();
		initData();
	}

	private void initData() {
		try {
			List<FeedBackBeen> fbs = db.findAll(FeedBackBeen.class);
			mList.addAll(fbs);
			mAdapter.notifyDataSetChanged();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/** 发送消息 **/
	public void onSend(View v) {
		String content = mPublicFeedBack.getEditableText().toString().trim();
		if (TextUtils.isEmpty(content)) {
			ZokeToast.show(getApplicationContext(), "请输入内容");
			return;
		}
		mPublicFeedBack.getEditableText().clear();
		mConversion.addUserReply(content);
		mConversion.sync(this);
	}

	@Override
	public void onReceiveDevReply(List<DevReply> replyList) {
		try {
			for (DevReply reply : replyList) {
				FeedBackBeen fb = new FeedBackBeen();
				fb.setContent(reply.getContent());
				fb.setType(0);
				fb.setTime(reply.getDatetime().getTime());
				db.save(fb);
				mList.add(fb);
			}
		} catch (Exception e) {
			LogUtil.e(getClass().getSimpleName(), e);
		}
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void onSendUserReply(List<Reply> replyList) {
		try {
			for (Reply reply : replyList) {
				FeedBackBeen fb = new FeedBackBeen();
				fb.setContent(reply.getContent());
				fb.setType(1);
				fb.setTime(reply.getDatetime().getTime());
				db.save(fb);
				mList.add(fb);
			}
		} catch (Exception e) {
			LogUtil.e(getClass().getSimpleName(), e);
		}
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		CommonUtil.hideKeyboard(this, mPublicFeedBack);
		return super.dispatchTouchEvent(ev);
	}
}
